/*! phonebar v1.0.0 | (c) created 20190327 | hejm@huayunworld.com */
define(["jquery", "phone"], function($) {
    jQuery.fn.phopanel = function(parentClass, phone, explicitNum) {
        var $parent = this;
        var phohtml = [
            '<div class="pho-panel ' + parentClass + '">',
            '<input maxlength="13" type="text" placeholder="请输入电话号码" class="pho-input b">',
            '<i class="iconfont icon-bohaopanshanchu"></i>',
            '<i class="iconfont icon-bohaopan btn_callout"></i>',
            '<ul class="pho-bohaopan">',
            '<li class="pho-bohao-sub1">拨号盘<span class="iconfont icon-guanbi"></span></li>',
            '<li class="pho-bohao-sub2">',
            '<ul class="pho-bohao-btn" data-trigger="bohao">',
            '<li><a href="">1</a></li>',
            '<li><a href="">2</a></li>',
            '<li><a href="">3</a></li>',
            '<li><a href="">4</a></li>',
            '<li><a href="">5</a></li>',
            '<li><a href="">6</a></li>',
            '<li><a href="">7</a></li>',
            '<li><a href="">8</a></li>',
            '<li><a href="">9</a></li>',
            '<li><a href="">*</a></li>',
            '<li><a href="">0</a></li>',
            '<li><a href="">#</a></li>',
            '<li class="callTo btn_callout"><a href="" class="iconfont icon-bohao conBtn btn_callout"></a></li>',
            '</ul>',
            '</li>',
            '</ul>',
            '</div>'
        ];
        phohtml = phohtml.join('');
        $parent.html(phohtml);
        $parent.on("keyup", ".pho-input", function(e) {
            var $input = $(this);
            console.log('keyup')
            if (e.keyCode == 13 && $input.val().length > 0) { //判断是否为回车键 以及符合格式（正则）
                var phoNo = $input.val();
                if (parentClass === 'panel1') {
                    phone.defineEvent('btn_callout', { 'callNum': phoNo, 'showNum': explicitNum })
                } else {
                    if (phoNo.indexOf('sip:') === -1) {
                        phoNo = "Tel:" + phoNo;
                    }
                    phone.defineEvent('btn_consult', { 'consultType': 3, 'targetNum': phoNo });
                }
            }
        });
        $parent.on("click", "a.conBtn", function(e) {
                e.stopPropagation();
                e.preventDefault();
                var $a = $(e.target);
                var phoNo = $a.parent().parent().parent().parent().parent().children().first().val();
                // var phoNo = $('.pho-input').val();
                if (parentClass === 'panel1') {
                    console.log('1111111111111')
                    phone.defineEvent('btn_callout', { 'callNum': phoNo, 'showNum': explicitNum })
                } else {
                    console.log('22222222', phoNo)
                    if (phoNo.indexOf('sip:') === -1) {
                        phoNo = "Tel:" + phoNo;
                    }
                    console.log('ppppp目标号码', phoNo)
                    phone.defineEvent('btn_consult', { 'consultType': 3, 'targetNum': phoNo });
                }
                // alert('我是拨号盘确认按钮');
            })
            .on("click", '.pho-panel>.icon-bohaopan', function(e) {
                e.preventDefault();
                e.stopPropagation();
                var $this = $(this);
                //拨号盘应该用组件来做 首先写功能 然后封装组件
                if ($this.next().is(":hidden")) {
                    $this.next().show();
                } else {
                    $this.next().hide();
                }
            })
            .on("click", ".pho-bohao-sub1>.icon-guanbi", function(e) {
                e.stopPropagation();
                //关闭拨号盘
                $(this).parent().parent().hide();
            })
            .on("click", '.pho-panel>.icon-bohaopanshanchu', function() {
                var $this = $(this);
                var $input = $this.prev();
                //拨号删除
                if ($input.val() !== '') {
                    $input.val('');
                    $this.hide();
                }
            })
            //拨号输入
            .on("click", "[data-trigger=bohao]>li>a", function(e) {
                e.stopPropagation();
                e.preventDefault();
                var $a = $(e.target);
                phone.ucObj.SendDTMF($a.text());
                var $input = $a.parent().parent().parent().parent().parent().children().first();
                if ($input.val().length < 12) {
                    $input.val($input.val() + $a.html());
                }
                $input.next().show();
                $input.focus();
            })
    }
})
